package org.extreme.schooltalk.parsing;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SimpleParser extends DefaultHandler{
	
	HashMap<String,String> resultMap;
	Map<String,String> map;
	String keySet[];
	String currentValue;
	Boolean inItem = false;
	public SimpleParser(InputStream input,String[] keys){
		try {
			keySet = keys;
			new SaxHandler(this,input);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Map<String,String> getMap(){
		return this.resultMap;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if(inItem)
			currentValue = new String(ch,start,length);
	}
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {		
		super.endElement(uri, localName, qName);
		for(String s : keySet){
			if(localName.equals(s)){
				resultMap.put(s, currentValue);
				break;
			}
		}
		
	}
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		inItem = true;
		if(localName.equalsIgnoreCase(SaxHandler._ITEM)){
			resultMap = new HashMap<String,String>();
		}
	}
	
	
}
